<script>
import HeaderPage from "@/components/headerPage.vue";
import {loginUser} from "@/api/login";
import FooterPage from "@/components/footerPage.vue";

export default {
  name: "onePage",
  components: {FooterPage, HeaderPage},
  data() {
    return {
      //
      userList: '',
      imageUrl: '',
      activename: 'first' // 默认显示第一页
    };
  },
  mounted() {
    this.getInfo()
  },
  methods: {
    // 修改名字
    changeName() {
      // this.nickname =
    },
    getInfo() {
      loginUser().then(res => {
        // console.log('User', res)
        this.userList = res.data
        this.imageUrl = this.userList.avatarUrl
        // if (res.code === 0) {
        //   this.$store.state.isLogin = true
        //   this.$store.state.userInfolist = res.data
        //   console.log(this.$store.state.userInfolist)
        // } else {
        //   this.$store.state.isLogin = false
        // }

      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    // 切换table
    handleclick(tab, event) {
      console.log(tab, event);
      if (tab.index === 0) {
        // 发起网络请求 1
        console.log(tab.index)
      } else if (tab.index === 1) {
        console.log(tab.index)
      } else {
        console.log(tab.index)
      }
    }
  }
}
</script>

<template>
  <div>
    <header-page></header-page>
    <main>
      <el-tabs tab-position="left" @tab-click="handleclick" style="height: 200px; ">
        <el-tab-pane label="我的主页">
          <div>
            <h3>个人信息</h3>
            <div>
              <span>昵称</span>
              <button @click="changeName">修改昵称</button>
              <div>{{ this.userList.nickname }}</div>
            </div>
            <div>
              <span>手机号</span>
              <button>修改</button>
              <div>{{ this.userList.mobile }}</div>
            </div>
            <div>
              <span>密码</span>
              <button>修改</button>
              <div>
                <input type="password" v-model="this.userList.password" style="border: none">
              </div>
            </div>
            <!--修改头像-->
            <el-upload
                class="avatar-uploader"
                action="http://192.168.31.44:8080/api/api/pcUser/updata/userAvatar"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" :src="imageUrl" class="avatar" style="width: 200px;height: 200px">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-tab-pane>
        <el-tab-pane label="我的课程">配置管理</el-tab-pane>
        <el-tab-pane label="我的订单">角色管理</el-tab-pane>
      </el-tabs>
    </main>
    <footer-page></footer-page>
  </div>
</template>

<style scoped lang="less">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

* {
  margin: 0;
  padding: 0;
}

main {
  width: 1160px;
  margin: auto;
}
</style>
